<?php

namespace api\controller;

// +----------------------------------------------------------------------
// | 项目团队数据控制层
// +----------------------------------------------------------------------

use common\controller\VerifyController;
use common\service\ProjectMemberService;
use common\service\ProjectService;
use think\Request;

class ProjectMemberController extends VerifyController
{
    /**
     * @var ProjectService
     */
    private $projectService;
    /** @var ProjectMemberService */
    private $projectMemberService;

    public function __construct()
    {
        parent::__construct();
        $this->projectService = ProjectService::getInstance();
        $this->projectMemberService = ProjectMemberService::getInstance();
    }

    /**
     * 批量创建项目成员
     * @param Request $request
     * @return \think\Response
     */
    public function batchCreate(Request $request)
    {
        $param = $request->param();
        $roleCode = '';
        foreach ($param['data']['user_list'] as $userItem) {
            $roleCode = $userItem['role_code'];
            break;
        }
        $res = $this->projectService->batchCreateProjectMember($param['data'], $roleCode);
        return json(success_response("", $res));
    }

    /**
     * 单个创建项目成员
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function singleCreate(Request $request)
    {
        $param = $request->param();
        $res = $this->projectService->singleCreateProjectMember($param['data']);
        return json(success_response("", $res));
    }

    /**
     * 扫描已存在数据到项目成员
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function scanExistDataToProjectMember(Request $request)
    {
        $param = $request->param();
        $res = $this->projectService->scanExistDataToProjectMember($param['param']['project_id']);
        return json(success_response("", $res));
    }

    /**
     * 同步用户角色 (删除掉project member中多余角色)
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function syncUserRoleProjectMember(Request $request)
    {
        $param = $request->param();
        $res = $this->projectService->syncUserRoleProjectMember($param['data']['tenant_id'], $param['data']['role_code']);
        return json(success_response("", $res));
    }

    /**
     * 项目立项-添加成员
     * @param Request $request
     * @return \think\Response
     * @throws \Exception
     */
    public function projectInitiationAddUser(Request $request)
    {
        $param = $request->param();
        $res = $this->projectService->projectInitiationAddUser($param['data']);
        return json(success_response("", $res));
    }

    /**
     * 独立项目-同步角色id
     * @param Request $request
     * @return \think\Response
     */
    public function syncProjectMemberRoleId(Request $request)
    {
        $res = $this->projectService->syncProjectMemberRoleId();
        return json(success_response("", $res));
    }

    /**
     * 项目立项-管理列表
     * @return \think\Response
     * @throws \think\Exception
     */
    public function manageList(Request $request)
    {
        $param = $request->param();
        $res = $this->projectService->manageList($param['param']['filter'], $request->getUserId());
        return json(success_response("", $res));
    }

    /**
     * 多项目角色用户列表
     * @param Request $request
     * @return \think\Response
     */
    public function roleUserList(Request $request)
    {
        $param = $request->param()['param'];
        $filter = $param['filter'];
        $page = $param['page'] ?? [1, C("database.database_max_select_rows")];
        $res = $this->projectMemberService->roleUserList($param, $filter, $page);
        return json(success_response("", $res));
    }

    public function removeProjectMember(Request $request)
    {
        $param = $request->param();
        $res = $this->projectMemberService->removeProjectMember($param['param']['ids'] ?? [], $request->getUserId());
        return json(success_response("", $res));
    }
}
